package org.jeecg.modules.third.amap.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.third.amap.dto.GpsDTO;
import org.jeecg.modules.third.amap.dto.MapDirectionDTO;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.entity.PointDataListVO;
import org.jeecg.modules.wlhy.shipping.entity.WaybillRouteVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @author: duanlikao
 * @date: 2022年10月07日 11:45
 */
@Service
@Slf4j
public class HyShippingNoteAmapService {

    @Autowired
    private AmapService amapService;

    /**
     * 获取高德模拟轨迹
     * @param hyShippingNote
     * @return
     */
    public List<GpsDTO> getMapGps(HyShippingNote hyShippingNote ){
        if(hyShippingNote == null){
            throw new JeecgBootException("该运单信息不存在！");
        }
        return amapService.getMapGps(hyShippingNote);
    }

    /**
     * 获取高德模拟轨迹
     * @param order
     * @return
     */
    public MapDirectionDTO getMapGpsOrder(HyOrder order) {
        if (order == null) {
            throw new JeecgBootException("该货单信息不存在！");
        }

        return amapService.getMapGpsOrder(order);
    }


    /**
     * 获取高德模拟轨迹 转换数据格式
     * @param shippingNote
     * @return
     */
    public WaybillRouteVO getSimulateTrackQuery(HyShippingNote shippingNote) {
        WaybillRouteVO waybillRouteVO = new WaybillRouteVO();
        if (shippingNote == null) {
            return waybillRouteVO;
        }

        List<GpsDTO> gpsDTOS = this.getMapGps(shippingNote);
        if (gpsDTOS == null) {
            return waybillRouteVO;
        }
        if (CollectionUtils.isNotEmpty(gpsDTOS)) {
            List<PointDataListVO> pointDataListVOs = new ArrayList();
            List<List<String>> valueList = new ArrayList<>();
            for (GpsDTO gpsDTO : gpsDTOS) {
                PointDataListVO pointDataListVO = new PointDataListVO();
                pointDataListVO.setCs(60d);
                pointDataListVO.setLat(gpsDTO.getLat());
                pointDataListVO.setLng(gpsDTO.getLon());
                pointDataListVO.setReceiveTime(gpsDTO.getPositionTime());
                pointDataListVOs.add(pointDataListVO);

                List<String> valueListString = new ArrayList<>();
                valueListString.add(gpsDTO.getLon());
                valueListString.add(gpsDTO.getLat());
                valueList.add(valueListString);
            }
            waybillRouteVO.setLineArray(valueList);
            waybillRouteVO.setPointDataList(pointDataListVOs);
        }
        return waybillRouteVO;
    }


    /**
     * 获取高德模拟轨迹 转换数据格式
     * @param order
     * @return
     */
    public WaybillRouteVO getSimulateTrackQueryOrder(HyOrder order) {
        if (null == order) {
            log.error("获取高德模拟轨迹失败，货源不能为空");
            return null;
        }

        WaybillRouteVO waybillRouteVO = new WaybillRouteVO();
        MapDirectionDTO mapDirectionDTO = this.getMapGpsOrder(order);
        if (null == mapDirectionDTO) {
            return null;
        }

        //模拟轨迹的总里程数
        waybillRouteVO.setMileage(mapDirectionDTO.getDistance());
        //经纬度转换为坐标点
        if (CollectionUtils.isNotEmpty(mapDirectionDTO.getGpsDTOList())) {
            List<PointDataListVO> pointDataListVOs = new ArrayList();
            List<List<String>> valueList = new ArrayList<>();
            for (GpsDTO gpsDTO : mapDirectionDTO.getGpsDTOList()) {
                PointDataListVO pointDataListVO = new PointDataListVO();
                pointDataListVO.setCs(60d);
                pointDataListVO.setLat(gpsDTO.getLat());
                pointDataListVO.setLng(gpsDTO.getLon());
                pointDataListVO.setReceiveTime(gpsDTO.getPositionTime());
                pointDataListVOs.add(pointDataListVO);

                List<String> valueListString = new ArrayList<>();
                valueListString.add(gpsDTO.getLon());
                valueListString.add(gpsDTO.getLat());
                valueList.add(valueListString);
            }

            waybillRouteVO.setLineArray(valueList);
            waybillRouteVO.setPointDataList(pointDataListVOs);
        }

        return waybillRouteVO;
    }

}
